<?php

// +----------------------------------------------------------------------
// | Admin Plugin for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2024 ThinkAdmin [ thinkadmin.top ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/think-plugs-admin
// | github 代码仓库：https://github.com/zoujingli/think-plugs-admin
// +----------------------------------------------------------------------

declare(strict_types=1);

namespace app\paths\controller;

use app\common\model\paths\ApplianceInfoModel;
use app\common\model\paths\EmployInfoModel;
use think\admin\Controller;
use think\admin\extend\DataExtend;
use think\admin\helper\QueryHelper;
use think\admin\service\ProcessService;


/**
 * 实战详情控制器
 * @class Base
 * @package app\admin\controller
 */
class EmployInfo extends Controller
{
    /**
     * 章节列表
     * @auth true
     * @login true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index ()
    {
        $this->app_id = $this->request->get('app_id');

        EmployInfoModel::mQuery()->layTable(function () {

        }, function (QueryHelper $query) {


            $this->check_account = false;
            $this->check_user    = false;

            $query->where('app_id', $this->app_id)->order('type,sort', 'asc');
            // 数据列表搜索过滤
            $query->like('title')->equal('status')->dateBetween('create_at');
        });
    }

    protected function _index_page_filter (&$data)
    {
        $type_name = EmployInfoModel::getTypeName();

        // 菜单数据树数据变平化
        $data = DataExtend::arr2table($data);
        //$data = DataExtend::arr2tree($data);

        foreach ($data as &$item) {

            $item['type_name'] = '';

            if ($item['pid'] !== 0) {

                if ($item['level'] == 2) {
                    $item['title'] = 'ㅤ├ㅤ'.$item['title'];
                }
                if ($item['level'] == 3) {
                    $item['title'] = 'ㅤㅤㅤ├ㅤ'.$item['title'];
                }
                $item['type_name'] = $type_name[$item['type']] ?? '';
            }


        }

    }
    /**
     * 添加
     *
     * @return void
     */
    public function add ()
    {

        EmployInfoModel::mForm();
    }

    /**
     * 添加
     *
     * @return void
     */
    public function addSon ()
    {
        EmployInfoModel::mForm();
    }


    /**
     * 表单数据处理
     * @param array $data
     */
    protected function _form_filter(array &$data)
    {
        if ($this->request->isGet()) {
            $this->app_id = $this->request->get('app_id');
            $this->type_arr = EmployInfoModel::getTypeName();
            //$this->language = EmployInfoModel::getLanguage();
        }

        if ($this->request->isPost() && isset($data['file_path']) && !empty($data['file_path'])) {
            // 获得后缀
            $extension = pathinfo($data['file_path'], PATHINFO_EXTENSION);

            $data['language'] = $extension == 'class' ? 'java' : $extension;
        }
    }

    /**
     * 表单后置操作，返回的 data 为提交的数据
     * @param boolean $result
     * @param array $data
     * @Description
     * @author wangqinghui
     * @date 2025-01-06 15:08
     * @return void
     */
    protected function _form_result(bool $result, array $data)
    {
        if($result && !empty($data['type']) && ($data['type'] == 2)){
            // 获取子关卡分数
            $where = [
                ['pid', '=', $data['pid']],
                ['type', '=', 2]
            ];
            $total_gold = EmployInfoModel::mk()->where($where)->sum('gold');
            // 更新父关卡分数
            EmployInfoModel::mk()->where('id', $data['pid'])->update(['gold' => $total_gold]);
        }
    }

    /**
     * 编辑
     *
     * @return void
     */
    public function edit ()
    {
        EmployInfoModel::mForm();
    }


    /**
     * 修改用户状态
     * @auth true
     */
    public function state()
    {

        EmployInfoModel::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除系统用户
     * @auth true
     */
    public function remove()
    {
        EmployInfoModel::mDelete();
    }


    /**
     * 上传php 执行文件
     *
     */
    public function upload ()
    {

        $file     = $this->request->file('file');
        $fileName = $this->request->post('fileName');

        // 获得后缀和文件名
        $extension = pathinfo($fileName, PATHINFO_EXTENSION);
        $file_name = pathinfo($fileName, PATHINFO_FILENAME);

        // 构造文件路径
        $str        = mt_rand(100,999);
        $dir_name   = date('Ymd')."/{$this->adminUser['id']}_{$str}_{$file_name}";

        $paths      = $extension == 'class' ? "upload/employ/java/{$dir_name}" : "upload/employ/{$extension}/{$dir_name}";

        $file_path = public_path($paths);
        // 存储文件
        $file->move($file_path, $fileName);

        if ($extension == 'java') {
            // 解析 编译
            ProcessService::exec("javac {$file_path}{$fileName}");
            $fileName = $file_name.'.class';
        }

        $file_path = $paths.'/'.$fileName;

        $this->success("成功", "$file_path");
    }
}